package org.benjamin.tenancy.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.tenancy.domain.form.LoginBean;
import org.benjamin.tenancy.service.HousingService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

import javax.validation.Valid;

@Controller
public class HomeController {

    private Logger logger = LogManager.getLogger(HomeController.class.getName());

    private HousingService housingService;

    @RequestMapping(value = "index")
    public String index(){
    	logger.info("index方法");
        return "admin.index";
    }

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String toLogin(@ModelAttribute("loginBean") LoginBean loginBean){
        return "login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String doLogin(@Valid LoginBean loginBean, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
        	logger.info("客户输入信息有误！");
            return "login";
        }
//        UsernamePasswordToken token = new UsernamePasswordToken(loginBean.getUsername(),loginBean.getPassword());
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(token);
        return "redirect:index";
    }
    
    @ResponseBody
    @RequestMapping(value = "menu-form.json")
    public JsonNode menuBanse() throws IOException{
    	logger.info("读取JSON导航文件");
    	ClassPathResource classPathResource = new ClassPathResource("menu-base.json");
    	File file = classPathResource.getFile();
    	JsonNode jsonNode = new ObjectMapper().readTree(file);
    	return jsonNode;
    }

}

